package com.penghaisoft.microwms.materialhand.web.insidewarehouse.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.HandBaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.LockTypeDto;
import com.penghaisoft.microwms.common.dto.RawStorageSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.materialhand.web.insidewarehouse.service.IMaterialHandInsideWhService;


@RestController
@RequestMapping("insideWh/lock")
public class LockMaterialController extends HandBaseController {
	
	@Autowired
    private IMaterialHandInsideWhService materialHandInsideWhService;
	
	
    @PostMapping("lockType")
    public HandResp lockType(){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
    
        List<LockTypeDto> list = new ArrayList<>();
        LockTypeDto lockType= new LockTypeDto();
        lockType.setLockcode("4");
        lockType.setLockName("手工冻结");
        list.add(lockType);
        LockTypeDto lockType2= new LockTypeDto();
        lockType2.setLockcode("2");
        lockType2.setLockName("质量冻结");
        list.add(lockType2);
		result.setList(list);
		result.setMsg("成功，返回冻结原因");
		return result;
   
    }
	
    @PostMapping("scan")
    public HandResp lockMessage(@RequestBody RawStorageSn rawStorageSn){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
        //根据条码查询
        rawStorageSn.setActiveFlag("1");
        List<RawStorageSn> list = materialHandInsideWhService.querybarcodeExist(rawStorageSn);
        if(list.size() <= 0){
        	result.setMsg("该条码不存在");
        	result.setCode(Constant.FAIL);
        }else{
        	if(list.get(0).getBarcodeStatus().equals("2")){
        		result.setMsg("该条码已出库");
            	result.setCode(Constant.FAIL);
        	}else if(list.get(0).getBarcodeStatus().equals("1")){
        		result.setMsg("该条码已冻结");
            	result.setCode(Constant.FAIL);
        	}
        	else{
        		//返回信息
        		result.setList(list);
        		result.setMsg("该条码可进行冻结，请确认");
        	}
        }
        return result;
    }
    

    @PostMapping("lockconfig")
    public HandResp lockConfig(@RequestBody RawStorageSnDto rawStorageSnDto){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
        RawStorageSn barcodeStorageSn = new RawStorageSn();
        barcodeStorageSn.setBarcode(rawStorageSnDto.getBarcode());
        barcodeStorageSn.setActiveFlag("1");
        //根据条码查询
        List<RawStorageSn> list = materialHandInsideWhService.querybarcodeExist(barcodeStorageSn);
        if(list.size() <= 0){
        	result.setMsg("该条码不存在");
        	result.setCode(Constant.FAIL);
        }else{
        	if(list.get(0).getBarcodeStatus().equals("2")){
        		result.setMsg("该条码已出库");
            	result.setCode(Constant.FAIL);
        	}else if(list.get(0).getBarcodeStatus().equals("1")){
        		result.setMsg("该条码已冻结");
            	result.setCode(Constant.FAIL);
        	}else{
        		//开始冻结
    			Resp resp = new Resp();
    			RawStorageSn rawStorageSn = new RawStorageSn();
    			rawStorageSn.setStorageSnId(list.get(0).getStorageSnId());
    			rawStorageSn.setBarcode(rawStorageSnDto.getBarcode());
    			rawStorageSn.setBarcodeStatus("1");//冻结
    			rawStorageSn.setLockReason(rawStorageSnDto.getLockReason());//0手工冻结1质量冻结
    			rawStorageSn.setLastModifiedBy(rawStorageSnDto.getLoginName());
    			rawStorageSn.setGmtModified(new Date());
    			resp = materialHandInsideWhService.updateRawStorageSn(rawStorageSn);
    			result.setCode(resp.getCode());
    			if(resp.getCode().equals("0")){
    				result.setMsg(resp.getMsg());	
    			}
        	}
        }
        return result;
    }

}
